﻿using System;

namespace UniRx.Operators
{
	// Token: 0x0200029E RID: 670
	internal class DoOnCancelObservable<T> : OperatorObservableBase<T>
	{
		// Token: 0x06000EC4 RID: 3780 RVA: 0x0004544C File Offset: 0x0004384C
		public DoOnCancelObservable(IObservable<T> source, Action onCancel) : base(source.IsRequiredSubscribeOnCurrentThread<T>())
		{
			this.source = source;
			this.onCancel = onCancel;
		}

		// Token: 0x06000EC5 RID: 3781 RVA: 0x00045468 File Offset: 0x00043868
		protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
		{
			return new DoOnCancelObservable<T>.DoOnCancel(this, observer, cancel).Run();
		}

		// Token: 0x0400088F RID: 2191
		private readonly IObservable<T> source;

		// Token: 0x04000890 RID: 2192
		private readonly Action onCancel;

		// Token: 0x0200029F RID: 671
		private class DoOnCancel : OperatorObserverBase<T, T>
		{
			// Token: 0x06000EC6 RID: 3782 RVA: 0x00045477 File Offset: 0x00043877
			public DoOnCancel(DoOnCancelObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.parent = parent;
			}

			// Token: 0x06000EC7 RID: 3783 RVA: 0x00045488 File Offset: 0x00043888
			public IDisposable Run()
			{
				return StableCompositeDisposable.Create(this.parent.source.Subscribe(this), Disposable.Create(delegate
				{
					if (!this.isCompletedCall)
					{
						this.parent.onCancel();
					}
				}));
			}

			// Token: 0x06000EC8 RID: 3784 RVA: 0x000454B1 File Offset: 0x000438B1
			public override void OnNext(T value)
			{
				this.observer.OnNext(value);
			}

			// Token: 0x06000EC9 RID: 3785 RVA: 0x000454C4 File Offset: 0x000438C4
			public override void OnError(Exception error)
			{
				this.isCompletedCall = true;
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x06000ECA RID: 3786 RVA: 0x00045504 File Offset: 0x00043904
			public override void OnCompleted()
			{
				this.isCompletedCall = true;
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x04000891 RID: 2193
			private readonly DoOnCancelObservable<T> parent;

			// Token: 0x04000892 RID: 2194
			private bool isCompletedCall;
		}
	}
}
